/**************************************************************************************************
---------------------------------------------------------------------------------------------------
	Copyright (c) 2004, Jonathan Bagg
	All rights reserved.

	 Redistribution and use in source and binary forms, with or without modification, are permitted 
	 provided that the following conditions are met:

    * Redistributions of source code must retain the above copyright notice, this list of 
	  conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright notice, this list of 
	  conditions and the following disclaimer in the documentation and/or other materials provided 
	  with the distribution.
    * Neither the name of Jonathan Bagg nor the names of its contributors may be used to 
	  endorse or promote products derived from this software without specific prior written permission.

  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR 
  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY 
  AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 
  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 
  SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 
  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 
  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
  POSSIBILITY OF SUCH DAMAGE.
---------------------------------------------------------------------------------------------------
   Project name : Infidigm AVR Drivers
   Processor	: ATMega64, ATMega128
   File name    : pid.h
---------------------------------------------------------------------------------------------------
   Modifications: 

   Revision 1.0  2004/07/23 	Bagg
   - Finished for release
---------------------------------------------------------------------------------------------------
   Created      : 10 April 2004                Author(s) : Jonathan Bagg
---------------------------------------------------------------------------------------------------
   Pid Controller 
---------------------------------------------------------------------------------------------------
**************************************************************************************************/

/**************************************************************************************************
*   Define PIDFLOAT to make PID have Float gains (Comment out for Char gains)
**************************************************************************************************/
//#define PIDFLOAT

/**************************************************************************************************
*   Structure for PID Variables  (add 'struct pidvars pidMotor1, pidMotor2;' in your application)
**************************************************************************************************/
struct pidvars {
	signed int input;
	signed int output;
	signed int setpoint;
	signed int lasterror;
	signed int limit;
	signed int accumerror;
	unsigned int maxout;
	#ifdef PIDFLOAT
	 float pgain;
	 float igain;
	 float dgain;
	#else
	 unsigned char pgain;
	 unsigned char igain;
	 unsigned char dgain;
	#endif
	unsigned char deadband;
 };
 
/**************************************************************************************************
*
*   Function          : pid
*
*   Description       : Proportional Integral Derivative (PID) Controller for Motor Speed Control.
*						This PID Controller takes the pulse width measurement (width1 or width3) and 
*						converts it into pulses/second for feedback. It is fully "Signed" so 
*						'pidMotor2.setpoint = -75;' will run the motor in reverse and 
*						'pidMotor2.setpoint = 75;' will run the motor forward at 75 pulses/second.
*						Output = Kp*error + Ki*(sum of all errors) + Kd*(lasterror - error)
*						Call this function 10 - 50mS for motor speed control.
*
*	Char Gains		  :	When the algorithm is using 'char' gains, it is setup to have a gain from
*						0.0625 to 16.  The gain is = n / 16; where;
*						pgain = 16 would gave Kp = 1
*						pgain = 1 would gave Kp = 1/16 = 0.0625
*						pgain = 255 would gave Kp = 255/16 ~16
*						
*	Execution Time	  : char gains 	= 24 uS
*	  at 15.36 Mhz		float gains = 385 uS
*
*   Parameters        : (&pidMotor2) The Struct that contains the pid variables for the specific motor  
*
*   Functions Called  : None
*
*   Returns           : Calculated output to be passed to motor() function (-1023 to 1023)
*
**************************************************************************************************/
 signed int pid(struct pidvars *piddata);